package com.jielihaofeng.chat.controller;

import com.jielihaofeng.chat.entity.ChatGroupEntity;
import com.jielihaofeng.chat.query.ChatGroupQuery;
import com.jielihaofeng.chat.service.ChatGroupService;
import com.jielihaofeng.chat.vo.ChatGroupVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jielihaofeng.chat.convert.ChatGroupConvert;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
* 群组表
*
* @author 借力好风 949394048@qq.com
* @since 1.0.0 2022-07-22
*/
@RestController
@RequestMapping("chat/chatgroup")
@Tag(name="群组表")
@AllArgsConstructor
public class ChatGroupController {
    private final ChatGroupService chatGroupService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('chat:chatgroup:page')")
    public Result<PageResult<ChatGroupVO>> page(@Valid ChatGroupQuery query){
        PageResult<ChatGroupVO> page = chatGroupService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('chat:chatgroup:info')")
    public Result<ChatGroupVO> get(@PathVariable("id") Long id){
        ChatGroupEntity entity = chatGroupService.getById(id);

        return Result.ok(ChatGroupConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('chat:chatgroup:save')")
    public Result<String> save(@RequestBody ChatGroupVO vo){
        chatGroupService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('chat:chatgroup:update')")
    public Result<String> update(@RequestBody @Valid ChatGroupVO vo){
        chatGroupService.update(vo);

        return Result.ok();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('chat:chatgroup:delete')")
    public Result<String> delete(@PathVariable Long id){
        chatGroupService.delete(id);

        return Result.ok();
    }
}